﻿using System.Drawing;
using System.Xml;

namespace PhotoFramePublishingPlugin
{
    internal class ConfigurationSettingsModel : IConfigurationSettingsModel
    {
        public string DestinationRoot { get; set; }
        public ImageSize ImageSize { get; set; }
        public bool ShouldAddBorders { get; set; }
        public bool ShouldShortenFileNames { get; set; }

        internal ConfigurationSettingsModel(int maxWidth, int maxHeight, string destinationRoot, bool shouldAddBorders, bool shouldShortenFileNames)
        {
            DestinationRoot = destinationRoot;
            ImageSize = new ImageSize(null, maxWidth, maxHeight);   //< TODO: Pass name to the model as well?
            ShouldAddBorders = shouldAddBorders;
            ShouldShortenFileNames = shouldShortenFileNames;
        }

        public static ConfigurationSettingsModel FromPersistXml(XmlDocument persistXml)
        {
            Size initialMaxSize = PersistHelper.ReadDimensions(persistXml);
            string initialDestinationRoot = PersistHelper.ReadDestination(persistXml);
            bool initialShouldAddBorders = PersistHelper.ReadShouldAddBorders(persistXml);
            bool initialShouldShortenFileNames = PersistHelper.ReadShouldShortenFileNames(persistXml);

            return new ConfigurationSettingsModel(initialMaxSize.Width, initialMaxSize.Height, initialDestinationRoot, initialShouldAddBorders, initialShouldShortenFileNames);
        }

        public void WriteToPersistXml(XmlDocument persistXml)
        {
            PersistHelper.InitializeDocument(persistXml);
            PersistHelper.WriteDestination(persistXml, DestinationRoot);
            if (ImageSize.IsValid)
                PersistHelper.WriteDimensions(persistXml, ImageSize.Width, ImageSize.Height);
            // TODO: ClearDimensions?
            PersistHelper.WriteShouldAddBorders(persistXml, ShouldAddBorders);
            PersistHelper.WriteShouldShortenFileNames(persistXml, ShouldShortenFileNames);
        }

        public void WriteToSessionXml(XmlDocument sessionXml)
        {
            SessionHelper.WriteDestination(sessionXml, DestinationRoot);
            if (ImageSize.IsValid)
                SessionHelper.WriteMaxSize(sessionXml, ImageSize.Width, ImageSize.Height);
            else
                SessionHelper.ClearMaxSize(sessionXml);
            SessionHelper.WriteShouldAddBorders(sessionXml, ShouldAddBorders);
            SessionHelper.WriteShouldShortenFileNames(sessionXml, ShouldShortenFileNames);
        }
    }
}